/**
 * @file		dd_pmc_temp_sensor_if.h
 * @brief		PMC Temperature sensor IF driver
 * @note		None
 * @attention	None
 * 
 * <B><I>Copyright 2016 Socionext Inc.</I></B>
 */

/** @weakgroup dd_pmc_temp_sensor_if_overview
@{
	CM0 PMC - Temperature sensor IF Driver.<br>
	<br>
	This product has only one temperature sensor.<br>
	This will read the temperature data of the temperature sensor.<br>
	If the temperature exceeds the specified threshold value, and generates an interrupt.
@}*//* --- end of dd_pmc_temp_sensor_if_overview */

/** @weakgroup dd_pmc_temp_sensor_if_sequence
@{
	- @ref dd_pmc_temp_sensor_if_sequence_sect
	- @ref dd_pmc_temp_sensor_if_sample_sect
	<br>
	@section dd_pmc_temp_sensor_if_sequence_sect	Sequence of Temperature sensor IF
	@image html dd_pmc_temp_sensor_if.png
	<br>
	@section dd_pmc_temp_sensor_if_sample_sect Sample code
	The example of Temp_sensor_if is as follows.
	@code
	void Temp_sensor_if_Sample()
	{
		// All sensors same setting
		T_DD_PMC_TS_IF_ALL	ctrl_all;

		ctrl_all.ts_reset	= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_all.ts_en		= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_all.hys_temp	= 0x00;
		ctrl_all.ts_ct		= E_DD_PMC_TS_IF_CT_0;
		ctrl_all.irq_mask.bit.ts_irq_0		= D_DD_PMC_TS_IF_ENABLE_OFF;
		ctrl_all.irq_mask.bit.all_lf_irq	= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_all.irq_mask.bit.all_ln_irq	= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_all.irq_mask.bit.all_hn_irq	= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_all.irq_mask.bit.all_hf_irq	= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_all.irq_mask.bit.any_lf_irq	= D_DD_PMC_TS_IF_ENABLE_OFF;
		ctrl_all.irq_mask.bit.any_ln_irq	= D_DD_PMC_TS_IF_ENABLE_OFF;
		ctrl_all.irq_mask.bit.any_hn_irq	= D_DD_PMC_TS_IF_ENABLE_OFF;
		ctrl_all.irq_mask.bit.any_hf_irq	= D_DD_PMC_TS_IF_ENABLE_OFF;
		ctrl_all.hi_full_temp	= 0x00000280;	//  80 degree
		ctrl_all.hi_near_temp	= 0x00000230;	//  70 degree
		ctrl_all.lo_full_temp	= 0xFFFFFF60;	// -20 degree
		ctrl_all.lo_near_temp	= 0xFFFFFF10;	// -30 degree
		ctrl_all.callback		= sample_ts_if_cb;
		Dd_PMC_TS_IF_Set_Control_All_Sensors( &ctrl_all );

		// Sensor individual setting
		T_DD_PMC_TS_IF_EACH		ctrl_sensor_0;

		ctrl_sensor_0.ts_reset	= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_sensor_0.ts_en		= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_sensor_0.hys_temp	= 0x00;
		ctrl_sensor_0.ts_ct		= E_DD_PMC_TS_IF_CT_0;
		ctrl_sensor_0.irq_mask.bit.lf_irq	= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_sensor_0.irq_mask.bit.ln_irq	= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_sensor_0.irq_mask.bit.hn_irq	= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_sensor_0.irq_mask.bit.hf_irq	= D_DD_PMC_TS_IF_ENABLE_ON;
		ctrl_sensor_0.hi_full_temp	= 0x00000280;	//  80 degree
		ctrl_sensor_0.hi_near_temp	= 0x00000230;	//  70 degree
		ctrl_sensor_0.lo_full_temp	= 0xFFFFFF60;	// -20 degree
		ctrl_sensor_0.lo_near_temp	= 0xFFFFFF10;	// -30 degree
		ctrl_sensor_0.callback		= sample_ts_if_cb;
		Dd_PMC_TS_IF_Set_Control_Each_Sensor( &ctrl_sensor_0 );
	}
	@endcode

@}*//* --- end of dd_pmc_temp_sensor_if_sequence */

#ifndef _DD_PMC_TEMP_SENSOR_IF_H_
#define _DD_PMC_TEMP_SENSOR_IF_H_

/** @weakgroup dd_pmc_temp_sensor_if_definition
@{*/

#include "driver_common.h"


/*----------------------------------------------------------------------*/
/* Definition															*/
/*----------------------------------------------------------------------*/
#define D_DD_PMC_TS_IF_INPUT_PARAM_ERR	(D_DD_PMC_TS_IF | D_DDIM_INPUT_PARAM_ERROR)		/**< Input Parameter Error */
#define D_DD_PMC_TS_IF_STATUS_ABNORMAL	(D_DD_PMC_TS_IF | D_DDIM_STATUS_ABNORMAL)		/**< Abnormal Status */

/** Temperature Sensor IF function enable */
#define	D_DD_PMC_TS_IF_ENABLE_OFF	(0)		/**< disable		*/
#define	D_DD_PMC_TS_IF_ENABLE_ON	(1)		/**< enable			*/

/** Callback status */
#define	D_DD_PMC_TS_IF_INT_STATUS_LF	(0x00000001)		/**< Callback status when exceed low temperature Interrupt.				*/
#define	D_DD_PMC_TS_IF_INT_STATUS_LN	(0x00000002)		/**< Callback status when approach low temperature Interrupt.			*/
#define	D_DD_PMC_TS_IF_INT_STATUS_HN	(0x00000003)		/**< Callback status when approach high temperature Interrupt.			*/
#define	D_DD_PMC_TS_IF_INT_STATUS_HF	(0x00000004)		/**< Callback status when exceed high temperature Interrupt.			*/

#define	D_DD_PMC_TS_IF_INT_TYPE_ALL		(0x01000000)		/**< Callback status when interrupt for in case all of sensors.			*/
#define	D_DD_PMC_TS_IF_INT_TYPE_ANY		(0x02000000)		/**< Callback status when interrupt for alarm of one or more sensor.	*/
#define	D_DD_PMC_TS_IF_INT_TYPE_TS0		(0x00010000)		/**< Callback status when Interrupt for temperature sensor #0.			*/


/*----------------------------------------------------------------------*/
/* Enumeration															*/
/*----------------------------------------------------------------------*/
/** Temperature Sensor IF config. */
typedef enum {
	E_DD_PMC_TS_IF_CONFIG_EACH_SETTING = 0,	/**< each sensor setting	*/
	E_DD_PMC_TS_IF_CONFIG_ALL_SETTING		/**< All sensors setting	*/
} E_DD_PMC_TS_IF_CONFIG;

/** Temperature Sensor IF type. */
typedef enum {
	E_DD_PMC_TS_IF_TYPE_ALL = 0,			/**< All sensors			*/
	E_DD_PMC_TS_IF_TYPE_TS0					/**< Temperature Sensor#0	*/
} E_DD_PMC_TS_IF_TYPE;

/** Temperature Sensor IF CT setting. */
typedef enum {
	//											 1st cycle tile after enablement,	Cycle time 2nd later
	E_DD_PMC_TS_IF_CT_0 = 0,				/**< 4279.								4128	*/
	E_DD_PMC_TS_IF_CT_1,					/**< 14355.								14204	*/
	E_DD_PMC_TS_IF_CT_2,					/**< 26643.								26492	*/
	E_DD_PMC_TS_IF_CT_3,					/**< 51219.								51068	*/
	E_DD_PMC_TS_IF_CT_MAX
} E_DD_PMC_TS_IF_CT;

/*----------------------------------------------------------------------*/
/* Structure  															*/
/*----------------------------------------------------------------------*/
/** Temperature Sensor IF macro information structure. */
typedef struct {
	UCHAR		ts_num;					/**< the number of equipment of temperature sensor	*/
	UCHAR		macro_rev;				/**< Macro revision		*/
	USHORT		macro_id;				/**< Macro ID			*/
} T_DD_PMC_TS_IF_MACRO_INFO;

/** Temperature Sensor IF IRQ structure for common. */
typedef union {
	ULONG		word;	/**< All bits */
	struct {
		ULONG	ts_irq_0	:1;		/**< interrupt temperature alarm 0	*/
		ULONG				:15;	/**< Dummy	*/
		ULONG	all_lf_irq	:1;		/**< interrupt for in case all of sensors exceed low temperature	*/
		ULONG	all_ln_irq	:1;		/**< interrupt for in case all of sensors approach low temperature	*/
		ULONG	all_hn_irq	:1;		/**< interrupt for in case all of sensors approach high temperature	*/
		ULONG	all_hf_irq	:1;		/**< interrupt for in case all of sensors exceed high temperature	*/
		ULONG				:4;		/**< Dummy	*/
		ULONG	any_lf_irq	:1;		/**< interrupt for the low temperature excess alarm of one or more sensor	*/
		ULONG	any_ln_irq	:1;		/**< interrupt for the nearby low temperature alarm of one or more sensor	*/
		ULONG	any_hn_irq	:1;		/**< interrupt for the nearby high temperature alarm of one or more sensor	*/
		ULONG	any_hf_irq	:1;		/**< interrupt for the high temperature excess alarm of one or more sensor	*/
		ULONG				:4;		/**< Dummy	*/
	}bit;				/**< Bit field */
} U_DD_PMC_TS_IF_IRQ_ALL;

/** Temperature Sensor IF IRQ structure for sensor#0. */
typedef union {
	ULONG		word;	/**< All bits */
	struct {
		ULONG	lf_irq	:1;		/**< Interrupt of LF_IRQ	*/
		ULONG	ln_irq	:1;		/**< Interrupt of LN_IRQ	*/
		ULONG	hn_irq	:1;		/**< Interrupt of HN_IRQ	*/
		ULONG	hf_irq	:1;		/**< Interrupt of HF_IRQ	*/
		ULONG			:28;	/**< Dummy	*/
	}bit;				/**< Bit field */
} U_DD_PMC_TS_IF_IRQ_SENSOR;

/** Temperature Sensor IF common information structure. */
typedef struct {
	UCHAR						ts_reset;		/**< common reset setting for all sensors [disable(0x0), enable(0x1)]			*/
	UCHAR						ts_en;			/**< common enablement for all sensors [disable(0x0), enable(0x1)]				*/
	UCHAR						hys_temp;		/**< Hysteresis setting for all temperature sensors	[0 degree(0x00) to 31.875 dgree(0xFF)]*/
	E_DD_PMC_TS_IF_CT			ts_ct;			/**< measurement time setting for all sensors		*/
	U_DD_PMC_TS_IF_IRQ_ALL		irq_mask;		/**< mask setting for each interrupt				*/
	// xxx_temp setting range : -256.000 degree(0xFFFFF800) to 256 degree(0x000007FF).
	ULONG						hi_full_temp;	/**< Threshold temperature setting of the high temperature excess for all sensors	*/
	ULONG						hi_near_temp;	/**< Threshold temperature setting of the nearby high temperature for all sensors	*/
	ULONG						lo_full_temp;	/**< Threshold temperature setting of the low temperature excess for all sensors	*/
	ULONG						lo_near_temp;	/**< Threshold temperature setting of the nearby low temperature for all sensors	*/
	VP_CALLBACK					callback;		/**< Callback function pointer			*/
} T_DD_PMC_TS_IF_ALL;

/** Temperature Sensor IF each information structure. */
typedef struct {
	UCHAR						ts_reset;		/**< The reset setting for temperature sensor[0-5]. [disable(0x0), enable(0x1)]			*/
	UCHAR						ts_en;			/**< Enablement setting for temperature sensor[0-5]. [disable(0x0), enable(0x1)]		*/
	UCHAR						hys_temp;		/**< Hysteresis setting for all temperature sensors	[0 degree(0x00) to 31.875 dgree(0xFF)]*/
	E_DD_PMC_TS_IF_CT			ts_ct;			/**< Measurement time setting for temperature sensor[0-5].	*/
	U_DD_PMC_TS_IF_IRQ_SENSOR	irq_mask;		/**< mask setting for each interrupt						*/
	// xxx_temp setting range : -256.000 degree(0xFFFFF800) to 256 degree(0x000007FF).
	ULONG						hi_full_temp;	/**< The high temperature excess threshold temperature of temperature sensor[0-5].	*/
	ULONG						hi_near_temp;	/**< Threshold temperature setting of the nearby high temperature for temperature sensors[0-5].	*/
	ULONG						lo_full_temp;	/**< Threshold temperature setting of the low temperature excess for temperaturel sensors[0-5].	*/
	ULONG						lo_near_temp;	/**< Threshold temperature setting of the nearby low temperature for temperature sensor[0-5].	*/
	VP_CALLBACK					callback;		/**< Callback function pointer			*/
} T_DD_PMC_TS_IF_EACH;

/** Temperature Sensor IF TS_DATA sensor[0-5] information structure. */
typedef struct {
	UCHAR						ts_data_valid;	/**< Temperature data valid/invalid setting.	*/
	ULONG						ts_data;	/**< Threshold temperature setting of the high temperature excess for all sensors	*/
} T_DD_PMC_TS_IF_TS_DATA;

/*----------------------------------------------------------------------*/
/* Grobal Data															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Macro																*/
/*----------------------------------------------------------------------*/
// Nothing Special

/* @} */	// dd_pmc_temp_sensor_if_definition group

/** @weakgroup dd_pmc_temp_sensor_if_api
@{*/
/*----------------------------------------------------------------------*/
/* Function																*/
/*----------------------------------------------------------------------*/
#ifdef __cplusplus
extern "C" {
#endif

/**
Get macro information.<br>
@param [out]	macro_info		Temperature Sensor IF macro information structure. See @ref T_DD_PMC_TS_IF_MACRO_INFO.
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TS_IF_INPUT_PARAM_ERR	Input Parameter Error
*/
extern INT32 Dd_PMC_TS_IF_Get_Macro_Info( T_DD_PMC_TS_IF_MACRO_INFO* const macro_info );

/**
Set All sensors setting enablement simultaneously.<br>
@param [in]	config			Temperature Sensor IF config information structure. See @ref E_DD_PMC_TS_IF_CONFIG.
@retval VOID
*/
extern VOID Dd_PMC_TS_IF_Set_All_Config( E_DD_PMC_TS_IF_CONFIG config );

/**
Get All sensors setting enablement simultaneously.<br>
@param [out]	config			Temperature Sensor IF config information structure. See @ref E_DD_PMC_TS_IF_CONFIG.
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TS_IF_INPUT_PARAM_ERR	Input Parameter Error
*/
extern INT32 Dd_PMC_TS_IF_Get_Config_Mode( E_DD_PMC_TS_IF_CONFIG* const config );

/**
Set Reset.<br>
@param [in]		ts_type			Temperature Sensor IF type.<br>
								<ul><li>@ref E_DD_PMC_TS_IF_TYPE_ALL
									<li>@ref E_DD_PMC_TS_IF_TYPE_TS0</ul>
@param [in]		reset			Reset setting.<br>
								0:reset enablement<br>
								1:reset release
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TS_IF_INPUT_PARAM_ERR	Input Parameter Error
@retval D_DD_PMC_TS_IF_STATUS_ABNORMAL	Abnormal Status
*/
extern INT32 Dd_PMC_TS_IF_Reset( E_DD_PMC_TS_IF_TYPE ts_type, UCHAR reset );

/**
Set control information of all Sensors.<br>
@param [in]		ctrl_all		Temperature Sensor IF all sensor information structure. See @ref T_DD_PMC_TS_IF_ALL.
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TS_IF_INPUT_PARAM_ERR	Input Parameter Error
*/
extern INT32 Dd_PMC_TS_IF_Set_Control_All_Sensors( T_DD_PMC_TS_IF_ALL const *const ctrl_all );

/**
Get control information of all Sensors.<br>
@param [out]		ctrl_all	Temperature Sensor IF all sensor information structure. See @ref T_DD_PMC_TS_IF_ALL.
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TS_IF_INPUT_PARAM_ERR	Input Parameter Error
*/
extern INT32 Dd_PMC_TS_IF_Get_Control_All_Sensors( T_DD_PMC_TS_IF_ALL* const ctrl_all );

/**
Set control information of Temperature Sensor#0.<br>
@param [in]		ctrl_param		Temperature Sensor IF sensor information structure. See @ref T_DD_PMC_TS_IF_EACH.
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TS_IF_INPUT_PARAM_ERR	Input Parameter Error
*/
extern INT32 Dd_PMC_TS_IF_Set_Control_Each_Sensor( T_DD_PMC_TS_IF_EACH const *const ctrl_param );

/**
Get control information of Temperature Sensor#0.<br>
@param [out]	ctrl_param		Temperature Sensor IF sensor information structure. See @ref T_DD_PMC_TS_IF_EACH.
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TS_IF_INPUT_PARAM_ERR	Input Parameter Error
*/
extern INT32 Dd_PMC_TS_IF_Get_Control_Each_Sensor( T_DD_PMC_TS_IF_EACH* const ctrl_param );

/**
Get TS Data information.<br>
@param [out]	ts_data		Temperature Sensor IF sensor TS_DATA structure. See @ref T_DD_PMC_TS_IF_TS_DATA.
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TS_IF_INPUT_PARAM_ERR	Input Parameter Error
*/
extern INT32 Dd_PMC_TS_IF_Get_TS_Data( T_DD_PMC_TS_IF_TS_DATA* const ts_data );

/**
Get IRQOUT status information of all sensors.<br>
@param [out]	irq_out		Temperature Sensor IF IRQOUT structure. See @ref U_DD_PMC_TS_IF_IRQ_ALL.
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TS_IF_INPUT_PARAM_ERR	Input Parameter Error
*/
extern INT32 Dd_PMC_TS_IF_Get_IRQ_Out_Status_All_Sensors( U_DD_PMC_TS_IF_IRQ_ALL* const irq_out );

/**
Get IRQOUT status information of Temperature Sensor#0.<br>
@param [out]	irq_out		Temperature Sensor IF IRQOUT structure. See @ref U_DD_PMC_TS_IF_IRQ_SENSOR.
@retval D_DDIM_OK						OK
@retval D_DD_PMC_TS_IF_INPUT_PARAM_ERR	Input Parameter Error
*/
extern INT32 Dd_PMC_TS_IF_Get_IRQ_Out_Status_Each_Sensor( U_DD_PMC_TS_IF_IRQ_SENSOR* const irq_out );

/**
The interruption flag is cleared, and the CallBack function is called.<br>
@retval None
*/
extern VOID Dd_PMC_TS_IF_Int_Handler( VOID );

#ifdef __cplusplus
}
#endif

/* @} */	// dd_pmc_temp_sensor_if_api group

#endif	// _DD_PMC_TEMP_SENSOR_IF_H_
